﻿using System;
using Controllers;
using EntityLayer;
using EntityLayer.Entities;
using Utils;

public partial class ProfilePage : BasePage, IUserManagementView
{
    private UserController supervisingController; 
    public string LoggedInUserName
    {
        get
        {
            if (IsUserLoggedIn)
                return LoggedInUser.UserName;
            else
                return string.Empty;
        }
    }
    /// <summary>
    /// anonymous access allowed
    /// </summary>
    protected override Enumerations.UserPrivileges UserPrivileges
    {
        get { return Enumerations.UserPrivileges.Null; }
    }

    #region IUserManagementView Members
    public override void BindData()
    {
        TogglePanelVisibility();
    }
    public event EventHandler<UserEventArgs> CreateUserEvent;
    public event EventHandler<UserEventArgs> UpdateUserEvent;
    #endregion

    protected override void InitializeSupervisingController()
    {
        supervisingController = new UserController(this);
    }
    private void ClearAllPanels()
    {
        pnlLogin.Visible = false;
        pnlMainMenu.Visible = false;
        pnlProfile.Visible = false;
        pnlCreateNewUser.Visible = false;
    }
    private void TogglePanelVisibility()
    {
        ClearAllPanels();
        if (IsUserLoggedIn)
            pnlProfile.Visible = true;
        else
            pnlMainMenu.Visible = true;
    }
    private void btnCreateNewUser_Click(object sender, EventArgs e)
    {
        User user = new User(txtNewUserName.Text, Crypto.Hash(txtNewUserPassword.Text));
        try
        {
            CreateUserEvent(this, new UserEventArgs(user));
        }
        catch (Exception exp)
        {
            lblErr.Text = exp.Message;
            return;
        }
        lblErr.Text = string.Empty;
        LoggedInUser = user;
        TogglePanelVisibility();
    }
    private void btnShowNewUserPanel_Click(object sender, EventArgs e)
    {
        ClearAllPanels();
        pnlCreateNewUser.Visible = true;
    }
    private void btnShowLoginPanel_Click(object sender, EventArgs e)
    {
        ClearAllPanels();
        pnlLogin.Visible = true;
    }
    private void btnLogin_Click(object sender, EventArgs e)
    {
        ClearAllPanels();
        User user = EntityLayer.Entities.User.GetUserByPassword(txtUserName.Text, txtPassword.Text);
        if (user == null)
            lblErr.Text = "Login unsuccessful for user [" + txtUserName.Text + "]";
        else
            LoggedInUser = user;
        TogglePanelVisibility();
    }
    protected override void WireEvents()
    {
        btnLogin.Click += new EventHandler(btnLogin_Click);
        btnShowLoginPanel.Click += new EventHandler(btnShowLoginPanel_Click);
        btnShowNewUserPanel.Click += new EventHandler(btnShowNewUserPanel_Click);
        btnCreateNewUser.Click += new EventHandler(btnCreateNewUser_Click);
    }
}